package com.java.alibaba.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.content.Media;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

@RequestMapping("/muti")
@RestController
public class MutiModelController {
    private final ChatClient chatClient;

    public MutiModelController(ChatClient.Builder chatClient) {
        this.chatClient = chatClient.build();
    }

    @RequestMapping("/image")
    public String image(String prompt) throws URISyntaxException, MalformedURLException {
        String url = "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg";
        List<Media> mediaList = List.of(new Media(MimeTypeUtils.IMAGE_JPEG, new URI(url).toURL().toURI()));
        //定义用户提示词
        UserMessage userMessage = UserMessage.builder().text(prompt).media(mediaList).build();
        //调用AI模型
        return this.chatClient.prompt(new Prompt(userMessage)).call().content();
    }
}
